package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.CxSelect;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EvtType;
import com.ruoyi.system.service.IEvtTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 事件类型Controller
 *
 * @author ruoyi
 * @date 2024-08-18
 */
@Controller
@RequestMapping("/system/type")
public class EvtTypeController extends BaseController {
    private String prefix = "system/type";

    @Autowired
    private IEvtTypeService evtTypeService;

    @RequiresPermissions("system:type:view")
    @GetMapping()
    public String type() {
        return prefix + "/type";
    }

    /**
     * 查询事件类型列表
     */
    @RequiresPermissions("system:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EvtType evtType) {
        startPage();
        List<EvtType> list = evtTypeService.selectEvtTypeList(evtType);
        return getDataTable(list);
    }

    /**
     * 导出事件类型列表
     */
    @RequiresPermissions("system:type:export")
    @Log(title = "事件类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EvtType evtType) {
        List<EvtType> list = evtTypeService.selectEvtTypeList(evtType);
        ExcelUtil<EvtType> util = new ExcelUtil<EvtType>(EvtType.class);
        return util.exportExcel(list, "事件类型数据");
    }

    /**
     * 新增事件类型
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存事件类型
     */
    @RequiresPermissions("system:type:add")
    @Log(title = "事件类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EvtType evtType) {
        return toAjax(evtTypeService.insertEvtType(evtType));
    }

    /**
     * 修改事件类型
     */
    @RequiresPermissions("system:type:edit")
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap) {
        EvtType evtType = evtTypeService.selectEvtTypeByTypeId(typeId);
        mmap.put("evtType", evtType);
        return prefix + "/edit";
    }

    /**
     * 修改保存事件类型
     */
    @RequiresPermissions("system:type:edit")
    @Log(title = "事件类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EvtType evtType) {
        return toAjax(evtTypeService.updateEvtType(evtType));
    }

    /**
     * 删除事件类型
     */
    @RequiresPermissions("system:type:remove")
    @Log(title = "事件类型", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(evtTypeService.deleteEvtTypeByTypeIds(ids));
    }

    /**
     * 加载部门列表树
     */
    @RequiresPermissions("system:type:list")
    @GetMapping("/evtTypeTreeData")
    @ResponseBody
    public List<Ztree> evtTypeTreeData() {
        List<Ztree> ztrees = evtTypeService.selectTypeTree(new EvtType());
        return ztrees;
    }

    /**
     * 选择部门树
     *
     * @param evtTypeId 事件类型ID
     */
    @RequiresPermissions("system:type:list")
    @GetMapping("/selectEvtTypeTree/{evtTypeId}")
    public String selectEvtTypeTree(@PathVariable("evtTypeId") Long evtTypeId, ModelMap mmap) {
        //default evt_type
        EvtType evtType = null;
        if (evtTypeId != null && evtTypeId > 0) {
            evtType = evtTypeService.selectEvtTypeByTypeId(evtTypeId);
        }
        if (evtType == null) {
            evtType = new EvtType();
        }
        mmap.put("evtType", evtType);

        return prefix + "/evtTypeTree";
    }

    /**
     * 加载部门列表树
     */
    @RequiresPermissions("system:type:list")
    @GetMapping("/evtTypeCxSelectData")
    @ResponseBody
    public List<CxSelect> evtTypeCxSelectData() {
        List<CxSelect> cxSelects = evtTypeService.selectCxSelect(new EvtType());
        return cxSelects;
    }
}
